package com.yuexun.beilunpatient.ui.bodyCheck.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.view.ProgressWebView;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.utils.PreferenceHelper;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2019/4/8 0008.
 */

public class Frag_Basic extends KJFragment implements View.OnKeyListener{

    @Bind(R.id.webview)
    ProgressWebView webview;

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View view = inflater.inflate(R.layout.frag_webview, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initData() {
        init_web();
        try {
            webview.loadUrl("https://51wangzhen.com/piv/simulationLogin?examView=true&examType=basic&identity="+
                    PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.ID_CARD));
        } catch (Exception e) {
            Toast.makeText(getActivity(), "信息加载错误", Toast.LENGTH_SHORT).show();
            return;
        }
    }

    @SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"})
    private void init_web() {
        webview.setOnKeyListener(this);
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setDomStorageEnabled(true);
        webview.setInitialScale(100);
        webview.setWebViewClient(new CustomWebViewClient());
//        webview.setWebChromeClient(new CustomWebChromeView());
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return false;
    }

    private class CustomWebViewClient extends WebViewClient {


        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("tel:")) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                startActivity(intent);
                return true;
            } else {
//                view.loadUrl(url);
                return false;
            }
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }


        @Override
        public void onPageFinished(WebView view, String url) {

            super.onPageFinished(view, url);
        }
    }

}
